package com.yunkeji.system.service.impl;

import cn.hutool.core.bean.BeanUtil;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.enums.app.CheckStatus;
import com.yunkeji.common.utils.JsonUtils;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.dao.domain.UserFeedback;
import com.yunkeji.dao.vo.UserFeedbackVo;
import com.yunkeji.system.domain.bo.CheckBo;
import com.yunkeji.system.domain.bo.UserFeedbackBo;
import com.yunkeji.system.mapper.AdminUserFeedbackMapper;
import com.yunkeji.system.service.IUserFeedbackService;

import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 反馈Service业务层处理
 *
 * @author zhang
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class UserFeedbackServiceImpl implements IUserFeedbackService {

    private final AdminUserFeedbackMapper baseMapper;

    /** 查询反馈 */
    @Override
    public UserFeedbackVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /** 查询反馈列表 */
    @Override
    public TableDataInfo<UserFeedbackVo> queryPageList(UserFeedbackBo bo, PageQuery pageQuery) {
        Page<UserFeedbackVo> result = baseMapper.selectPageList(pageQuery.build(), bo);
        List<UserFeedbackVo> vos = result.getRecords();
        vos.forEach(vo ->
            vo.setImages(StringUtils.isEmpty(vo.getImgs()) ? Collections.emptyList() : JsonUtils.parseArray(vo.getImgs(), String.class))
        );
        result.setRecords(vos);
        return TableDataInfo.build(result);
    }

    /** 查询反馈列表 */
    @Override
    public List<UserFeedbackVo> queryList(UserFeedbackBo bo) {
        LambdaQueryWrapper<UserFeedback> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserFeedback> buildQueryWrapper(UserFeedbackBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserFeedback> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getType() != null, UserFeedback::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), UserFeedback::getContent, bo.getContent());
        lqw.eq(
                StringUtils.isNotBlank(bo.getContract()),
                UserFeedback::getContract,
                bo.getContract());
        lqw.eq(StringUtils.isNotBlank(bo.getImgs()), UserFeedback::getImgs, bo.getImgs());
        lqw.eq(bo.getUserId() != null, UserFeedback::getUserId, bo.getUserId());
        lqw.eq(bo.getStatus() != null, UserFeedback::getStatus, bo.getStatus());
        return lqw;
    }

    /** 新增反馈 */
    @Override
    public Boolean insertByBo(UserFeedbackBo bo) {
        UserFeedback add = BeanUtil.toBean(bo, UserFeedback.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /** 修改反馈 */
    @Override
    public Boolean updateByBo(UserFeedbackBo bo) {
        UserFeedback update = BeanUtil.toBean(bo, UserFeedback.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /** 保存前的数据校验 */
    private void validEntityBeforeSave(UserFeedback entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /** 批量删除反馈 */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void pass(CheckBo bo) {
        UserFeedback userFeedback = baseMapper.selectById(bo.getId());
        userFeedback.setStatus(CheckStatus.审核通过.getCode());
        userFeedback.setRemark(bo.getReason());
        baseMapper.updateById(userFeedback);
    }

    @Override
    public void refuse(CheckBo bo) {
        UserFeedback userFeedback = baseMapper.selectById(bo.getId());
        userFeedback.setStatus(CheckStatus.审核拒绝.getCode());
        userFeedback.setRemark(bo.getReason());
        baseMapper.updateById(userFeedback);
    }
}
